﻿using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Core.Projects;
using QDT.MVC.Security;
using QDT.Services.Projects;
using QDT.Web.Admin.Models;

namespace QDT.Web.Admin.Controllers
{
    [SecurityModule(Name = "担保机构")]
    public class ProjectAgencyController : BaseController
    {
        private readonly IProjectService _projectService;

        public ProjectAgencyController(IProjectService projectService)
        {
            _projectService = projectService;
        }

        [SecurityNode(Name = "担保机构列表")]
        public ActionResult Index(int? page, GridSortOptions sort)
        {
            const int pageSize = 10;
            int pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;

            var source = DbProvider.D<ProjectAgency>().OrderBy(sort.Column, sort.Direction = SortDirection.Ascending);
            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "添加担保机构")]
        public ActionResult AddAgency()
        {
            return View();
        }

        [HttpPost]
        [SecurityNode(Name = "添加担保机构操作")]
        public ActionResult PostAddAgency(ProjectAgencyModel model)
        {
            var agency = new ProjectAgency()
                {
                    Name = model.Name,
                    SubName = model.SubName,
                    Address = model.Address,
                    Details = model.Details,
                    Legal = model.Legal,
                    RegisteredCapital = model.RegisteredCapital,
                    StateAndCity = model.StateAndCity,
                    CreateDate = model.CreateDate,
                    CooperateDate = model.CooperateDate,
                    Introduction = model.Introduction,
                    CooperateAmount = model.CooperateAmount,
                    Logo = model.Logo
                };
            DbProvider.Add(agency);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }

        [SecurityNode(Name = "编辑担保机构")]
        public ActionResult EditAgency(int id)
        {
            return View(DbProvider.GetByID<ProjectAgency>(id));
        }

        [HttpPost]
        [SecurityNode(Name = "编辑担保机构操作")]
        public ActionResult PostEditAgency(ProjectAgencyModel model)
        {
            var agency = DbProvider.GetByID<ProjectAgency>(model.Id);
            if (agency == null) return Error();

            agency.Name = model.Name;
            agency.SubName = model.SubName;
            agency.Address = model.Address;
            agency.Details = model.Details;
            agency.Legal = model.Legal;
            agency.RegisteredCapital = model.RegisteredCapital;
            agency.StateAndCity = model.StateAndCity;
            agency.CreateDate = model.CreateDate;
            agency.CooperateDate = model.CooperateDate;
            agency.Introduction = model.Introduction;
            agency.CooperateAmount = model.CooperateAmount;
            agency.Logo = model.Logo;

            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        [SecurityNode(Name = "图片")]
        public ActionResult Images(int id)
        {
            var result = _projectService.GetProjectAgencyImages(id);
            return View(result);
        }

        [SecurityNode(Name = "添加图片")]
        public ActionResult AddImage(int id)
        {
            return View();
        }

        [HttpPost]
        [SecurityNode(Name = "添加图片操作")]
        public ActionResult PostAddImage(int aid, string title, string thumbnail, string photo, int sort)
        {
            var img = new ProjectImage(0, title, photo, sort)
            {
                Thumbnail = thumbnail,
                AgencyId = aid
            };
            DbProvider.Add(img);
            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        [SecurityNode(Name = "删除图片")]
        public ActionResult DeleteImage(int[] id)
        {
            foreach (var i in id)
            {
                var feild = DbProvider.GetByID<ProjectImage>(i);
                if (feild == null) continue;
                DbProvider.Delete(feild);
            }
            DbProvider.SaveChanges();
            return Success("操作成功");
        }

        [SecurityNode(Name = "编辑图片")]
        public ActionResult EditImage(int id)
        {
            var img = DbProvider.GetByID<ProjectImage>(id);
            if (img == null) return Error("资源不存在");

            return View(img);
        }

        [HttpPost]
        [SecurityNode(Name = "编辑图片操作")]
        public ActionResult PostEditImage(int id, string title, string thumbnail, string photo, int sort)
        {
            var img = DbProvider.GetByID<ProjectImage>(id);
            if (img == null) return Error("资源不存在");

            img.Title = title;
            img.Thumbnail = thumbnail;
            img.Photo = photo;
            img.Sort = sort;

            DbProvider.SaveChanges();

            return Success("操作成功");
        }

    }
}
